package com.boyinet.activation.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by tao.
 * Date: 2021/6/28 16:37
 * 描述:
 */
public class CDKeyEncryptUtils {
	    //--------------AES---------------
	    private static final String KEY = "BOYIQD2259810966";  // 密匙，必须16位
	    private static final String OFFSET = "BOYIQD4346831798"; // 偏移量
	    private static final String ENCODING = "UTF-8"; // 编码
	    private static final String ALGORITHM = "AES"; //算法
	    private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding"; // 默认的加密算法，CBC模式


	    public static String AESencrypt(String data) throws Exception {
	        //指定算法、获取Cipher对象(DES/CBC/PKCS5Padding：算法为，工作模式，填充模式)
	        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
	        //根据自定义的加密密匙和算法模式初始化密钥规范
	        SecretKeySpec skeySpec = new SecretKeySpec(KEY.getBytes("ASCII"), ALGORITHM);
	        //CBC模式偏移量IV
	        IvParameterSpec iv = new IvParameterSpec(OFFSET.getBytes());
	        //初始化加密模式
	        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
	        //单部分加密结束，重置Cipher
	        byte[] encrypted = cipher.doFinal(data.getBytes(ENCODING));
	        //加密后再使用BASE64做转码

			StringBuilder stringBuilder = new StringBuilder();
			for (byte b : encrypted) {
				stringBuilder.append(String.format("%02X",b));
			}
	        return stringBuilder.toString();
	    }

}
